﻿/*
*救援力量管理子系统
*交通运输
*医疗卫生
*/
IF EXISTS(SELECT NAME FROM SYSOBJECTS WHERE NAME='Proc_GetTrafficHealths' AND TYPE='P')
BEGIN
  DROP PROCEDURE Proc_GetTrafficHealths
END
GO
CREATE PROCEDURE Proc_GetTrafficHealths
	@pageIndex int,
	@pageSize int,
	@deptName varchar(100),
	@region varchar(50),
	@type varchar(2)
	
 AS
DECLARE @SQL nvarchar(1000)
DECLARE @RecordCount int,@P1 INT
DECLARE @Where VARCHAR(200)
IF @PageIndex >1 SET @PageIndex = (@PageIndex-1)*@PageSize +1
SET @Where = ''
IF ((@region IS NOT NULL) AND (RTRIM(LTRIM(@region))<>''))
BEGIN
  SET @Where = @Where +  ' AND Region = '''+ @region + ''''
END
IF ((@type IS NOT NULL) AND (RTRIM(LTRIM(@type))<>''))
BEGIN
  SET @Where = @Where +  ' AND Type = '''+ @type + ''''
END
IF (@deptName IS NOT NULL )  AND (RTRIM(LTRIM(@deptName))<>'')
BEGIN
	SET @Where = @Where +  ' AND DeptName LIKE ''%'+ @deptName + '%'''
END
IF ((LEN(@Where)>0) AND (LEFT(@Where,5)=' AND')) 
BEGIN
	SET @Where = Right(@Where,LEN(@Where)-5)
END
SET @SQL = N'SELECT * FROM  Eaer_TrafficHealth ORDER BY TraID DESC'
IF (LEN(@Where)>0)
BEGIN
  SET @SQL = 'SELECT * FROM  Eaer_TrafficHealth WHERE ' + @Where + ' ORDER BY TraID DESC'
END
EXEC sp_cursoropen @P1 OUTPUT, @SQL, @scrollopt = 1, @ccopt = 335873, @rowcount = @RecordCount OUTPUT 
IF (@P1 != 0)
BEGIN
  EXEC sp_cursorfetch @P1, 32, @PageIndex, @PageSize 
  EXEC sp_cursorclose @P1
END
select @RecordCount
return @@ERROR
 